/*
Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const MainLayout = () => import('../components/Layout/Main');

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        name: '乾初字画',
        component: MainLayout,
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: '首页',
                component: () => import('../components/Page/Home'),
                meta: {title: '艺秀文化'}
            },
            {
                path: '/item',
                name: '条款',
                component: () => import('../components/Page/Item'),
                meta: {title: '公告'}
            },
            {
                path: '/center',
                name: '我的',
                component: () => import('../components/Page/Center'),
                meta: {title: '我的', wx_auth: true}
            },
        ]
    },
    {
        path: '/post-detail/:id',
        name: '公告详情',
        component: () => import('../components/Page/PositDetail'),
        meta: {title: '公告详情'}
    },
    {
        path: '/collection-list/:vid',
        name: '藏品列表',
        component: () => import('../components/Page/CollectionList'),
        meta: {title: '商品列表'}
    },
    {
        path: '/collection-detail/:vcid',
        name: '藏品详情',
        component: () => import('../components/Page/CollectionDetail'),
        meta: {title: '商品详情', wx_auth: true}
    },
    {
        path: '/order-confirm/:vcid',
        name: '订单确认',
        component: () => import('../components/Page/OrderConfirm'),
        meta: {title: '订单确认', wx_auth: true}
    },
    {
        path: '/order-detail/:order_no',
        name: '订单支付',
        component: () => import('../components/Page/OrderDetail'),
        meta: {title: '订单支付', wx_auth: true}
    },
    {
        path: '/order-list',
        name: '订单列表',
        component: () => import('../components/Page/OrderList'),
        meta: {title: '订单', wx_auth: true}
    },
    {
        path: '/order-ship',
        name: '发货订单',
        component: () => import('../components/Page/OrderShip'),
        meta: {title: '发货订单', wx_auth: true}
    },
    {
        path: '/address/:order_id',
        name: '我的地址',
        component: () => import('../components/Page/Address'),
        meta: {title: '我的地址', wx_auth: true}
    },
    {
        path: '/address',
        name: '我的地址2',
        component: () => import('../components/Page/Address'),
        meta: {title: '我的地址', wx_auth: true}
    },
    {
        path: '/address-edit/:id',
        name: '编辑地址',
        component: () => import('../components/Page/AddressEdit'),
        meta: {title: '编辑地址', wx_auth: true}
    },
    {
        path: '/address-edit',
        name: '添加地址',
        component: () => import('../components/Page/AddressEdit'),
        meta: {title: '添加地址', wx_auth: true}
    },
    {
        path: '/promote',
        name: '推广名片',
        component: () => import('../components/Page/Promote'),
        meta: {title: '推广名片', wx_auth: true}
    },
    {
        path: '/invite/:promote_code',
        name: '二维码渠道邀请',
        component: () => import('../components/Page/Invite'),
        meta: {title: '二维码渠道邀请', wx_auth: true}
    },
    {
        path: '/invite',
        name: '二维码渠道邀请2',
        component: () => import('../components/Page/Invite'),
        meta: {title: '二维码渠道邀请', wx_auth: true}
    },
    {
        path: '/member',
        name: '我的粉丝',
        component: () => import('../components/Page/Member'),
        meta: {title: '我的粉丝', wx_auth: true}
    },
    {
        path: '/member-order',
        name: '粉丝订单',
        component: () => import('../components/Page/MemberOrder'),
        meta: {title: '粉丝订单', wx_auth: true}
    },
    {
        path: '/collect-setting',
        name: '收款设置',
        component: () => import('../components/Page/CollectSetting'),
        meta: {title: '收款设置', wx_auth: true}
    },
    {
        path: '/wallet',
        name: '钱包',
        component: () => import('../components/Page/Wallet'),
        meta: {title: '钱包', wx_auth: true}
    },
    {
        path: '/fee-detail',
        name: '佣金明细',
        component: () => import('../components/Page/FeeDetail'),
        meta: {title: '佣金明细', wx_auth: true}
    },
    {
        path: '/withdraw',
        name: '提现信息',
        component: () => import('../components/Page/Withdraw'),
        meta: {title: '提现信息', wx_auth: true}
    },
    {
        path: '/wx-auth',
        name: '微信授权',
        component: () => import('../components/Page/WxAuth'),
        meta: {title: '', wx_auth: true}
    },
    {
        path: '/pay/:order_no',
        name: '支付',
        component: () => import('../components/Page/Pay'),
        meta: {title: ''}
    },
    {
        path: '/signup',
        name: '注册',
        component: () => import('../components/Page/Signup'),
        meta: {title: '', wx_auth: true}
    },
    {
        path: '/login',
        name: '登录',
        component: () => import('../components/Page/Login'),
        meta: {title: ''}
    },
    {
        path: '/password',
        name: '修改密码',
        component: () => import('../components/Page/Password'),
        meta: {title: '', wx_auth: true}
    },

];

const createRouter = () => new VueRouter({
    mode: 'history', // require service support
    base: process.env.BASE_URL,
    scrollBehavior: () => ({y: 0}),
    routes: routes
});

const Router = createRouter();

Router.beforeEach((to, from, next) => {
    // console.log('[route]To: ' + to.name)
    // console.log('[route]From: ' + from.name)
    // if (from.name == 'historyrecord' && (to.name == 'modefreepage' || to.name == 'modecountpage' || to.name == 'modetimepage')) {
    //     window.history.go(-2);
    //     window.location.replace();
    //     // next({name: 'index'});
    //     return;
    // }
    if (to.meta.title) {
        document.title = to.meta.title
    }

    if (to.path === '/login') {
        let wx_openid = localStorage.getItem('wx_openid');
        if (wx_openid) {
            next('/');
            return;
        }
    }

    if (to.meta.wx_auth !== undefined && to.meta.wx_auth === true) {
        // process.env.NODE_ENV === 'production'

        if (to.path === '/wx-auth') {
            next();
            return;
        }

        // const wx_token = localStorage.getItem('wx_token');
        let wx_openid = localStorage.getItem('wx_openid');
        if (!wx_openid) {

            localStorage.setItem("now_url", to.path);

            if (to.fullPath.includes('/invite')) {
                let host = 'http://qczh.xiuyiwenhua.com';
                let randomParams = '&state=lajiweixin'; // 随机参数（可不传）
                let redirect_uri = encodeURIComponent(`${host}/wx-auth`);
                localStorage.setItem('promote_code', to.params.promote_code);
                window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5c938f141335a996&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo${randomParams}#wechat_redirect`;
                return;
            }

            next('/login');
            return;

            ///////
            // var ua = window.navigator.userAgent.toLowerCase();
            // //通过正则表达式匹配ua中是否含有MicroMessenger字符串
            // if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            //     let host = 'http://qczh.xiuyiwenhua.com';
            //     let randomParams = '&state=lajiweixin'; // 随机参数（可不传）
            //     let redirect_uri = encodeURIComponent(`${host}/wx-auth`);
            //
            //     if (to.fullPath.includes('/invite')) {
            //         localStorage.setItem('promote_code', to.params.promote_code);
            //         // redirect_uri = encodeURIComponent(`${host}${to.path}`);
            //         // redirect_uri = encodeURIComponent(`${host}/invite`);
            //     }
            //
            //     window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5c938f141335a996&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo${randomParams}#wechat_redirect`;
            //     return;
            //
            //     // if (to.path === '/wx-auth' ) {
            //     //     next();
            //     // } else if (to.fullPath.includes('/invite')){
            //     //     localStorage.setItem('now_url', to.fullPath); //当前页url与参数放入缓存
            //     //     localStorage.setItem('promote_code', to.params.promote_code);
            //     //     next();
            //     // } else {
            //     //     localStorage.setItem('now_url', to.fullPath); //当前页url与参数放入缓存
            //     //     next('/wx-auth');
            //     // }
            //     // return;
            // } else {
            //     next('/login');
            //     return;
            // }
            /////

            // 处理URL地址栏所携带的参数
            // let search = window.location.search;
            // if (search.includes("params_key") && search.includes("&")) {
            //     search = search.split("&")[0];
            // }


        }
    }

    next();
});

export default Router
